package com.xinhua.erp.service;

import com.xinhua.erp.pojo.PageResult;
import com.xinhua.erp.pojo.ProcessCompound;
import com.xinhua.erp.pojo.Result;
import com.xinhua.erp.repo.ProcessCompoundRepo;
import com.xinhua.erp.utils.IdWorker;
import com.xinhua.erp.utils.OrderNumber;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;


@Service
@Transactional
public class ProcessCompoundService {

    @Autowired
    private ProcessCompoundRepo processCompoundRepo;

    @Autowired
    private IdWorker idWorker;

    public Result updateState(Long id){

        processCompoundRepo.updateState(id);

        return new Result(true,20000,"审核成功");

    }

    public Result findById(long id){

        return Result.OK(processCompoundRepo.getOne(id));

    }



    public Result findAllByType(String type,int page ,int size){

        Pageable pageable = PageRequest.of(page-1, size);

        Page<ProcessCompound>processCompounds=processCompoundRepo.findAllByType(type,pageable);

        PageResult<ProcessCompound> pageResult = new PageResult<>(processCompounds.getTotalElements(),processCompounds.getContent());

        return Result.OK(pageResult);

    }

    public Result findAllByTypeAndState(String type,String state,int page,int size){

        Pageable pageable = PageRequest.of(page-1, size);

        Page<ProcessCompound> processCompounds = processCompoundRepo.findAllByTypeAndState(type,state,pageable);

        PageResult<ProcessCompound> pageResult = new PageResult<>(processCompounds.getTotalElements(),processCompounds.getContent());

        return Result.OK(pageResult);
    }

    public Result deleteById(long id){

        processCompoundRepo.deleteById(id);

        return Result.OK();

    }

    public Result save(ProcessCompound processCompound){

        processCompound.setId(idWorker.nextId());

        processCompound.setOrderId(OrderNumber.getOrderNumber());

        LocalDateTime now = LocalDateTime.now();

        processCompound.setCreatTime(now);

        processCompoundRepo.save(processCompound);

        return Result.OK();

    }

    public Result update(ProcessCompound processCompound,Long id){

        processCompound.setId(id);

        processCompoundRepo.save(processCompound);

        return Result.OK();

    }
    public Result Search(Map searchMap, int page, int size){
        Pageable pageable= PageRequest.of(page-1, size);
        Page<ProcessCompound> processCompounds= processCompoundRepo.findAll(createSpecification(searchMap),pageable);
        return Result.OK(new PageResult<>(processCompounds.getTotalElements(),processCompounds.getContent()));
    }

    private Specification<ProcessCompound> createSpecification(Map searchMap){
        return new Specification<ProcessCompound>() {
            @Override
            public Predicate toPredicate(Root<ProcessCompound> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> predicateList=new ArrayList<>();
                if(!StringUtils.isBlank((String)searchMap.get("state"))){

                    predicateList.add(criteriaBuilder.like(root.get("state").as(String.class),"%"+searchMap.get("state")+"%"));

                }
                if(!StringUtils.isBlank((String)searchMap.get("type"))){

                    predicateList.add(criteriaBuilder.like(root.get("type").as(String.class),"%"+searchMap.get("type")+"%"));

                }
                if(!StringUtils.isBlank((String)searchMap.get("orderId"))){

                    predicateList.add(criteriaBuilder.like(root.get("orderId").as(String.class),"%"+searchMap.get("orderId")+"%"));

                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }


}
